<template>
  <div class="goodInfoContainer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品信息</span>
      </div>
      <el-table
        :cell-style="{textAlign:'center'}"
        :data="goodData"
        :header-cell-style="{textAlign:'center'}"
        style="width: 100%"
      >
        <el-table-column
          label="商品ID"
          prop="id"
        />
        <el-table-column
          label="商品名称"
          prop="name"
        />
        <el-table-column
          label="图片"
          prop="image"
        >
          <template slot-scope="scope">
            <el-popover placement="right" title="" trigger="click">
              <img :src="scope.row.image"  style="height: 200px;width: 200px"/>
              <img slot="reference" :src="scope.row.image" :alt="scope.row.image" style="height: 50px;width: 50px">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="商品价格"
          prop="price"
        />

        <el-table-column
          label="规格属性"
          prop="spec"
        />
        <el-table-column
          label="品牌名称"
          prop="brandName"
        />
        <el-table-column
          label="类别名称"
          prop="categoryName"
        />
        <el-table-column
          label="状态"
          prop="status"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.status==='1'">正常</span>
            <span v-else-if="scope.row.status==='2'">下架</span>
            <span v-else>删除</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          :render-header="renderHeader"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="del(scope.row)">删除</el-button>
            <el-button size="mini" type="success" @click="update(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="queryParams.total>0"
        style="margin: 20px 0;float: right"
        :total="queryParams.total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getGoodInfoData"
      />
    </el-card>
  </div>
</template>

<script>
import { delGoodInfo, getGoodInfo } from '@/api/goods'

export default {
  name: 'GoodInfo',
  data() {
    return {
      // 分页查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      goodData: []
    }
  },
  created() {
    this.getGoodInfoData()
  },
  methods: {
    // 获取商品信息分页数据
    getGoodInfoData() {
      getGoodInfo().then(res => {
        this.queryParams.total = res.data.total
        this.goodData = res.data.records
        this.goodData.forEach(item => {
          item.price = item.price / 100
          item.image = item.images ? item.images.split(',')[0] : ''
        })
      })
    },
    renderHeader(h) {
      return (
        <div>操作
          <el-button type='success' size='mini' onClick={() => this.handleAddItem()}
            style='margin-left:20px'>添加</el-button>
        </div>
      )
    },
    // 跳转到添加商品页面
    handleAddItem() {
      this.$router.push('/addGoodInfo')
    },
    //  跳转到修改商品信息页面
    update(row) {
      console.log('editGoodInfo', row.id)
      this.$router.push({
        name: 'editGoodInfo',
        query: {
          id: row.id
        }
      })
    },
    //  删除数据
    del(row) {
      // 获取当前信息的ID，并赋值给全局ID变量
      this.id = row.id
      this.$confirm('您确定删除这条数据么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //  调用删除商品信息接口
        return delGoodInfo(this.id)
      }).then(() => {
        this.$message.success('删除成功')
        //  刷新数据列表
        this.getGoodInfoData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.goodInfoContainer {
  padding: 20px;
}
</style>
